package org.cypress.rtpack.common.core;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import lombok.Data;

@Data
public class Result<T> implements Serializable {

	/**
	 * 成功失败码 0:成功 1:失败
	 */
	private Integer code;

	/**
	 * 成功失败消息
	 */
	private String msg;

	/**
	 * 返回的业务对象
	 */
	private T result;

	/**
	 * 返回的业务Map对象
	 */
	//private Map<String, Object> resultMap = new HashMap<String, Object>();

	public static final String PAGE_INFO = "pageInfo";

	public static final Integer SUCCESS_CODE = 0;

	public static final Integer FAIL_CODE = 1;

	/**
	 * 默认构造方法
	 */
	public Result() {
	}

	public Result(Integer code) {
		this.code = code;
	}

	public Result(Integer code, String msg) {
		this.code = code;
		this.msg = msg;
	}
	/**
	 * 静态方法：快速构建一个成功的 Result，状态码为 0，消息为 "success"，data 为传入的对象
	 */
	public static <T> Result<T> ok(T data) {
		Result<T> res = new Result<>();
		res.code = SUCCESS_CODE;  // 0
		res.msg = "success";      // 成功提示
		res.result = data;        // 你类中的字段名叫 result
		return res;
	}
	/**
	 * 静态方法：返回一个成功的空响应，code=0，msg="success"，result=null（不传业务数据）
	 */
	public static Result<Void> ok() {
		Result<Void> res = new Result<>();
		res.code = SUCCESS_CODE; // 0
		res.msg = "success";     // 成功提示
		// result 默认就是 null，无需设置
		return res;
	}
	/**
	 * 静态方法：返回一个失败的响应，包含错误信息，状态码为 FAIL_CODE (1)
	 */
	public static Result<Void> fail(String message) {
		Result<Void> res = new Result<>();
		res.code = FAIL_CODE;    // 1 表示失败
		res.msg = message;       // 传入的错误提示，如 "添加趋势图失败"
		return res;
	}
	public static Result dataOf(Object response) {
		Result result = new Result();
		result.result = response;
		result.code = SUCCESS_CODE;
		result.msg = "success";
		return result;
	}
}
